package eiisan.controller.task;

import com.fasterxml.jackson.core.JsonProcessingException;
import eiisan.constant.settings.DefDesc;
import eiisan.dao.domain.task.Task;
import eiisan.service.task.base.BaseTaskService;
import eiisan.service.task.dto.AvailableTaskDTO;
import eiisan.util.json.JSONUtil;
import eiisan.util.model.response.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * 任务控制器
 **/
@RestController
@RequestMapping("/task")
@Api(tags = "任务控制器")
public class TaskController {

    @Autowired
    private BaseTaskService taskService;

    @GetMapping("/availables")
    @ApiOperation(value = "查询可用定时任务接口")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = AvailableTaskDTO.class)})
    public Result getAvailableTasks(@ApiParam(value = DefDesc.Message.PAGE_QUERY, required = true) Pageable param) {
        try {
            System.out.println(JSONUtil.string(param));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return taskService.getAvailableTasks(param);
    }

    @GetMapping("/runnings")
    @ApiOperation(value = "查询正在运行定时任务接口")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Task.class)})
    public Result getRunningTaskInfo(@ApiParam(value = DefDesc.Message.PAGE_QUERY, required = true) Pageable param) {
        return taskService.getRunningTasksInfo(param);
    }

    @GetMapping("/saves")
    @ApiOperation(value = "查询已保存定时任务接口")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Task.class)})
    public Result getTasks(@ApiParam(value = DefDesc.Message.PAGE_QUERY, required = true) Pageable param) {
        try {
            System.out.println(JSONUtil.string(param));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return taskService.getTasks(param);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加新定时任务接口")
    public Result addTask(@RequestBody @ApiParam(value = "新增定时任务请求参数", required = true) Task param) {
        return taskService.addTask(param);
    }

    @PostMapping("/cancel/all")
    @ApiOperation(value = "取消所有定时任务接口")
    public Result cancelAll() {
        return taskService.cancelAllTasks();
    }

    @PostMapping("/change")
    @ApiOperation(value = "更改定时任务活跃状态接口")
    public Result changeStatus(@RequestParam("id") @ApiParam(value = "任务ID", required = true) Long id,
                               @RequestParam("status") @ApiParam(value = "是否立刻启动 1:立刻启动 2:暂不启动", required = true) Byte status) {
        return taskService.changeStatus(id, status);
    }

    @DeleteMapping
    @ApiOperation(value = "删除定时任务接口")
    public Result deleteTask(@RequestParam("id") @ApiParam(value = "任务ID", required = true) Long id) {
        return taskService.deleteTask(id);
    }


}
